package com.young.controller;

import com.young.config.SysConfig;
import com.young.entity.User;
import com.young.exception.NoDataException;
import com.young.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Decoder;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 描述:
 *
 * @author BluceYoung
 * @create 2020-12-04 15:24
 * B站：TeacherYoung凌风
 * www.teacheryoung.com
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/edit.html")
    public String edit(){
        return "user/edit";
    }

    @PostMapping("/edit.html")
    public String doedit(User user, Model model,HttpSession session){

        try {
            userService.modify(user);
            session.setAttribute(SysConfig.SESSION_USER,user);
            model.addAttribute("msg","保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg","保存失败");
        }
        return "user/edit";
    }

    @GetMapping("/head.html")
    public String head(){
        return "user/head";
    }

    @ResponseBody
    @PostMapping(value = "/uploadhead",produces = "application/json;charset=utf-8")
    public String uploadHead(String imgBase64,
                             HttpSession session,
                             @SessionAttribute(SysConfig.SESSION_USER) User user){
        imgBase64 = imgBase64.split(",")[1];
        try {
            String savePath = session.getServletContext().getRealPath("/WEB-INF/statics/images/userhead");
            //调用业务方法
            String imgPath = userService.uploadHead(user.getId(), imgBase64, savePath);
            user.setHeadImg(imgPath);
            session.setAttribute(SysConfig.SESSION_USER,user);
            return "头像修改成功";
        }catch (Exception e){
            return "头像修改失败，"+e.getMessage();
        }
    }


    @GetMapping(value = {"/profile/{userId}","/profile/"})
    public String profile(@PathVariable(value = "userId",required = false) Long userId,
                          Model model,
                          @SessionAttribute(SysConfig.SESSION_USER) User sessionUser){
        //如果userId不为空，并且userId不是当前登录用户的编号，再查
        if(userId!=null && userId.longValue()!=sessionUser.getId().longValue()){
            User condition=new User();
            condition.setId(userId);
            User user = userService.get(condition);//根据用户编号查询一个用户
            if(user!=null){//查到其他用户的数据了
                //把其他用户的数据放入request，注意：不能替换session数据
                model.addAttribute(SysConfig.SESSION_USER,user);
            }else{
                throw new NoDataException("用户不存在");
            }
        }
        return "user/profile";
    }
}
